// releasePage/pages/publishQuestion/publishQuestion.js
import throttle from '../../../utils/throttle.js';
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        review: '', //审核编辑标记位
        // 上传照片
        sceneImageArr: [],
        // 是否匿名，默认不匿名
        anonymous: false,
        pjteamid: '',
        title: '',
        description: '',
        id: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            pjteamid: options.id,
            id: options.questionId,
            review: options.review || '',
        });
        if (options.questionId) {
            this.getQuestionDetail(options.questionId);
        }
    },
    //标题输入
    titleValue(e) {
        this.setData({
            title: e.detail.value,
        });
        app.numLimitTip(e.detail.value.length, 60);
    },
    //标题输入
    contentValue(e) {
        this.setData({
            description: e.detail.value,
        });
    },
    //再次编辑获取通告内容
    getQuestionDetail(id) {
        let _that = this;
        wx.request({
            url:
                app.globalData.rootPath +
                `cjMap/getProjectQuestionDetail?authId=${app.globalData.authId}&id=${id}`,
            method: 'GET',
            header: {
                'content-type': 'application/json',
            },
            success: function (res) {
                console.log(res.data.projectQuestion);
                _that.setData({
                    description: res.data.projectQuestion.description,
                    title: res.data.projectQuestion.title,
                    sceneImageArr: res.data.projectQuestion.picurls || [],
                    anonymous:
                        res.data.projectQuestion.anonymousflg == '01'
                            ? true
                            : false,
                });
            },
        });
    },
    // 添加图片
    addImg(e) {
        const that = this;
        wx.chooseImage({
            count: 8 - that.data.sceneImageArr.length,
            success(res) {
                let imgLen =
                    that.data.sceneImageArr.length + res.tempFilePaths.length;
                if (imgLen > 8) {
                    wx.showModal({
                        content: '最多上传8张图片，请重新上传',
                        showCancel: false,
                    });
                    return;
                } else {
                    wx.showLoading({
                        title: '图片上传中',
                        mask: true,
                    });
                    const imgUploadResults = [];
                    let index = that.data.sceneImageArr.length;
                    for (let i = 0; i < res.tempFilePaths.length; i++) {
                        let imgUploadResult = app
                            .imgUpload(res.tempFilePaths[i])
                            .then((res) => {
                                let num = i + index;
                                that.data.sceneImageArr[num] = res;
                                that.setData({
                                    sceneImageArr: that.data.sceneImageArr,
                                });
                                console.log(that.data.sceneImageArr);
                            });
                        imgUploadResults.push(imgUploadResult);
                    }
                    Promise.all(imgUploadResults).then((values) => {
                        wx.hideLoading();
                    });
                }
            },
        });
    },
    // 删除图片
    deleteImg(e) {
        let index = e.currentTarget.dataset.index;
        console.log(index);
        this.data.sceneImageArr.splice(index, 1);
        this.setData({
            sceneImageArr: this.data.sceneImageArr,
        });
    },
    // 预览图片
    previewImage(e) {
        const current = e.target.dataset.src;
        wx.previewImage({
            current,
            urls: this.data.sceneImageArr,
        });
    },

    // 切换匿名
    toggle() {
        this.setData({
            anonymous: !this.data.anonymous,
        });
    },
    submit: throttle(
        function (e) {
            let _that = this;
            if (app.visitorPublish()) return; //游客发布限制
            if (!_that.data.title) return;
            // wx.showModal({
            //     title: '确认提交？',
            //     content: '',
            //     success: function (res) {
            //         if (res.confirm) {

            //         } else {
            //             console.log('点击取消回调');
            //         }
            //     },
            // });
            if (_that.data.id) {
                wx.request({
                    url:
                        app.globalData.rootPath +
                        `cjMap/editProjectQuestion?authId=${app.globalData.authId}`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                    },
                    data: {
                        delflg: '02', //删除标志 00未删除 01已删除 02待审核10草稿
                        pjteamid: _that.data.pjteamid, //项目组id
                        picurls: _that.data.sceneImageArr, //图片
                        description: _that.data.description, //正文
                        title: _that.data.title,
                        anonymousflg: _that.data.anonymous ? '01' : '00', //00不匿名 01匿名
                        id: _that.data.id,
                    },
                    success: function (res) {
                        console.log(res);
                        if (!_that.data.review) {
                            let pages = getCurrentPages(); //当前页面栈
                            let prevPage = pages[pages.length - 2]; //上一页面
                            prevPage.refreshQuestion();
                            let prevPage2 = pages[pages.length - 3]; //上一页面
                            if (prevPage2) {
                                prevPage2.refreshQuestion();
                            }
                        } else {
                            app.doVerify(res.data.data.id, '08');
                        }
                        wx.showToast({
                            title: '提交成功',
                            icon: 'success',
                            duration: 1500,
                        });
                    },
                });
            } else {
                wx.request({
                    url:
                        app.globalData.rootPath +
                        `cjMap/createProjectQuestion?authId=${app.globalData.authId}`,
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                    },
                    data: {
                        delflg: '02', //删除标志 00未删除 01已删除 02待审核10草稿
                        pjteamid: _that.data.pjteamid, //项目组id
                        picurls: _that.data.sceneImageArr, //图片
                        description: _that.data.description, //正文
                        title: _that.data.title,
                        anonymousflg: _that.data.anonymous ? '01' : '00', //00不匿名 01匿名
                    },
                    success: function (res) {
                        console.log(res);
                        let pages = getCurrentPages(); //当前页面栈
                        let prevPage = pages[pages.length - 2]; //上一页面
                        prevPage.refreshQuestion();
                        let prevPage2 = pages[pages.length - 3]; //上一页面
                        if (prevPage2) {
                            prevPage2.refreshQuestion();
                        }
                        wx.showToast({
                            title: '提交成功',
                            icon: 'success',
                            duration: 1500,
                        });
                    },
                });
            }
            if (!_that.data.review) {
                setTimeout(function () {
                    wx.navigateBack({
                        delta: 1, // 返回上一级页面。
                    });
                }, 2000);
            }
        },
        4000,
        {
            trailing: false,
        }
    ),
});
